package com.example.hotel_lxy.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;

public class SyncHorizontalScrollView extends HorizontalScrollView {
    private View mView;
    private Runnable scrollerTask;
    private int intitPosition;
    private int newCheck = 100;
    private int childWidth = 0;
    private float downX;
    private float endX;
    private boolean aBoolean = true;
    private boolean aBooleantwo = true;
    private ScrollViewListener scrollViewListener = null;
    public SyncHorizontalScrollView(Context context) {
        super(context);
    }

    public SyncHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }
    @Override

    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        //这是上方的水平标题与下方的水平内容进行绑定
        if(scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX=ev.getX();
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_MOVE:
                endX=ev.getX();
                break;
        }
        return super.onTouchEvent(ev);
    }


    public interface OnScrollStopListner
    {
        /**
         * scroll have stoped
         */
        void onScrollStoped();
        /**
         * scroll have stoped, and is at left edge
         */
        void onScrollToLeftEdge();
        /**
         * scroll have stoped, and is at right edge
         */
        void onScrollToRightEdge();
        /**
         * scroll have stoped, and is at middle
         */
        void onScrollToMiddle();
    }
    private OnScrollStopListner onScrollstopListner;

    public SyncHorizontalScrollView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        scrollerTask = new Runnable()
        {
            @Override
            public void run()
            {
                int newPosition = getScrollX();
                if (intitPosition - newPosition == 0)
                {
                    if(onScrollstopListner == null)
                    {
                        return;
                    }
                    onScrollstopListner.onScrollStoped();
                    Rect outRect = new Rect();
                    getDrawingRect(outRect);
                    if(getScrollX() == 0)
                    {
                        if(endX<downX){
                            onScrollstopListner.onScrollToRightEdge();
                        }else{
                            if (aBoolean) {
                                onScrollstopListner.onScrollToLeftEdge();
                                aBoolean = false;
                            }else{
                                aBoolean = true;
                            }
                        }
                    }
                    else if(childWidth + getPaddingLeft() + getPaddingRight() == outRect.right)
                    {
                        if (aBooleantwo) {
                            onScrollstopListner.onScrollToRightEdge();
                            aBooleantwo = false;
                        }else{
                            aBooleantwo = true;
                        }
                    }
                    else
                    {
                        aBoolean = false;
                        aBooleantwo = false;
                        onScrollstopListner.onScrollToMiddle();
                    }
                } else
                {
                    aBoolean = false;
                    aBooleantwo = false;
                    intitPosition = getScrollX();
                    postDelayed(scrollerTask, newCheck);
                }
            }
        };
    }

    public void setOnScrollStopListner(OnScrollStopListner listner)
    {
        onScrollstopListner = listner;
    }

    public void startScrollerTask()
    {
        intitPosition = getScrollX();
        postDelayed(scrollerTask, newCheck);
        checkTotalWidth();
    }
    private void checkTotalWidth()
    {
        childWidth=0;
        for(int i = 0; i < getChildCount(); i++)
        {
            childWidth += getChildAt(i).getWidth();
        }
    }
    public interface ScrollViewListener {

        void onScrollChanged(SyncHorizontalScrollView scrollView, int x, int y, int oldx, int oldy);

    }
}

